package erp_services

import (
	"errors"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/app/services/types"
	"xiehui-service/global"
)

type orderProductionService struct {
}

type OrderProductionResult struct {
	List         []erp_models.OrderProduction
	TotalPages   int64
	TotalRecords int64
}

var OrderProductionService = new(orderProductionService)

// 获取单条信息
func (orderProductionService *orderProductionService) GetOneInfo(id uint) (orderProduction *erp_models.OrderProduction, err error) {
	err = global.App.DB.First(&orderProduction, id).Error
	if err != nil {
		orderProduction = nil
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

// 根据唯一编号获取生产单
func (orderProductionService *orderProductionService) GetByUnioNo(unionNo string) (orderProduction erp_models.OrderProduction, err error) {
	err = global.App.DB.Where("union_no = ? ", unionNo).First(&orderProduction).Error

	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

/**
 * 获取生产单列表
 * @param lastTime 上次同步时间
 * @param vLineNumber 线号
 * @param flagType    同步标识 1,sync_flag;2,delivery_flag
 * @param pagination 分页
 */
func (orderProductionService *orderProductionService) GetListByLasttime(lastTime string, vLineNumber string, flagType string, pagination types.Pagination) (result *OrderProductionResult, err error) {
	cond := "line_number = ? AND task_id > 0 AND sync_flag = 0 AND updated_at > ? AND deleted_at is null"
	if flagType == "2" {
		cond = "line_number = ? AND task_id > 0 AND delivery_flag = 0 AND updated_at > ? AND deleted_at is null"
	} else if flagType == "3" {
		cond = "line_number = ? AND task_id > 0 AND delivery_flag = 1 AND updated_at > ? AND deleted_at is null"
	}
	query := global.App.DB.Model(&erp_models.OrderProduction{}).Where(cond, vLineNumber, lastTime)

	// 获取总记录数
	var totalRecords int64
	if err = query.Count(&totalRecords).Error; err != nil {
		return nil, err
	}

	// 设置分页查询条件
	offset := (pagination.PageNum - 1) * pagination.PageSize
	query = query.Order("ID DESC").Offset(int(offset)).Limit(int(pagination.PageSize))

	// 获取当前页记录数
	var currentOrderProductions []erp_models.OrderProduction
	if err = query.Find(&currentOrderProductions).Error; err != nil {
		return nil, err
	}

	// 计算总页数
	totalPages := (totalRecords + int64(pagination.PageSize) - 1) / int64(pagination.PageSize)

	return &OrderProductionResult{
		List:         currentOrderProductions,
		TotalPages:   totalPages,
		TotalRecords: totalRecords,
	}, nil
}

func (orderProductionService *orderProductionService) GetUnSyncByLasttime(lastTime string, vLineNumber string, pagination types.Pagination) (result *OrderProductionResult, err error) {
	query := global.App.DB.Model(&erp_models.OrderProduction{}).Where("line_number = ? AND task_id > 0 AND sync_flag = 0 AND created_at < ? AND deleted_at is null", vLineNumber, lastTime)

	// 获取总记录数
	var totalRecords int64
	if err = query.Count(&totalRecords).Error; err != nil {
		return nil, err
	}

	// 设置分页查询条件
	offset := (pagination.PageNum - 1) * pagination.PageSize
	query = query.Order("ID ASC").Offset(int(offset)).Limit(int(pagination.PageSize))

	// 获取当前页记录数
	var currentOrderProductions []erp_models.OrderProduction
	if err = query.Find(&currentOrderProductions).Error; err != nil {
		return nil, err
	}

	// 计算总页数
	totalPages := (totalRecords + int64(pagination.PageSize) - 1) / int64(pagination.PageSize)

	return &OrderProductionResult{
		List:         currentOrderProductions,
		TotalPages:   totalPages,
		TotalRecords: totalRecords,
	}, nil
}

// 新增生产单
func (orderProductionService *orderProductionService) Create(orderProduction erp_models.OrderProduction) (uint, error) {
	result := global.App.DB.Create(&orderProduction)
	if result.Error != nil {
		return 0, result.Error
	}
	return orderProduction.ID.ID, nil
}

func (orderProductionService *orderProductionService) Update(orderProduction erp_models.OrderProduction) (err error) {
	if err := global.App.DB.Model(&erp_models.OrderProduction{}).Where("ID = ?", orderProduction.ID.ID).Omit("ID", "UnionNo").Updates(&orderProduction).Error; err != nil {
		return err
	}
	return nil
}

// 根据ID更新数据
func (orderProductionService *orderProductionService) SetById(id uint, fields map[string]interface{}) (err error) {
	var orderProduction erp_models.OrderProduction
	if err = global.App.DB.First(&orderProduction, id).Error; err != nil {
		err = errors.New("未查询到生产单信息")
		return
	}

	if err = global.App.DB.Model(&orderProduction).Updates(fields).Error; err != nil {
		return
	}

	return nil
}
